using Sparrow.Server;
using Sparrow.Threading;
using Tests.Infrastructure;
using Voron.Util.PFor;
using Xunit;
using Xunit.Abstractions;

namespace FastTests.Corax.Bugs;

public class SimdCompressionEncodingTests : NoDisposalNeeded
{
    private static long[] Data = new[]
    {
        39720194052, 39720198148, 39720230916, 39720235012, 39720259588, 39728521220, 39728529412, 39728533508, 39728537604, 39728541700, 39728545796, 39728553988,
        39728558084, 39728570372, 39728574468, 39728578564, 39728582660, 39728586756, 39728590852, 39728594948, 39728603140, 39728607236, 39728619524, 39728623620,
        39728631812, 39728640004, 39736905732, 39736909828, 39736918020, 39736922116, 39736926212, 39736930308, 39736934404, 39736938500, 39736950788, 39736958980,
        39736963076, 39736971268, 39736975364, 39736979460, 39736983556, 39736987652, 39736995844, 39736999940, 39737008132, 39737012228, 39737016324, 39737020420,
        39737024516, 39737028612, 39745290244, 39745294340, 39745298436, 39745302532, 39745306628, 39745310724, 39745318916, 39745323012, 39745331204, 39745335300,
        39745339396, 39745347588, 39745355780, 39745359876, 39745363972, 39745368068, 39745372164, 39745380356, 39745384452, 39745388548, 39745392644, 39745396740,
        39745409028, 39745417220, 39745421316, 39753678852, 39753682948, 39753687044, 39753691140, 39753695236, 39753699332, 39753703428, 39753707524, 39753711620,
        39753715716, 39753719812, 39753732100, 39753744388, 39753756676, 39753764868, 39753768964, 39753773060, 39753785348, 39753789444, 39753793540, 39753809924,
        39753814020, 39762075652, 39762173956, 39770488836, 40005373956, 40005398532, 40005414916, 40005419012, 40005423108, 40005431300, 40005439492, 40005447684,
        40005468164, 40005480452, 40013725700, 40013729796, 40013742084, 40013746180, 40013754372, 40013762564, 40013766660, 40013770756, 40013778948, 40013783044,
        40013787140, 40013795332, 40013799428, 40013807620, 40013811716, 40013824004, 40013828100, 40013832196, 40013836292, 40013840388, 40013852676, 40022118404,
        40022122500, 40022126596, 40022130692, 40022134788, 40022147076, 40022155268, 40022159364, 40022175748, 40022179844, 40022183940, 40022188036, 40022192132,
        40022196228, 40022200324, 40022204420, 40022208516, 40022212612, 40022220804, 40022228996, 40022233092, 40022245380, 40030502916, 40030507012, 40030511108,
        40030515204, 40030523396, 40030531588, 40030535684, 40030539780, 40030543876, 40030552068, 40030556164, 40030560260, 40030568452, 40030572548, 40030576644,
        40030580740, 40030584836, 40030593028, 40030601220, 40030605316, 40030609412, 40030617604, 40030621700, 40030629892, 40030633988, 40038891524, 40038895620,
        40038899716, 40038903812, 40038912004, 40038916100, 40038928388, 40038932484, 40038936580, 40038944772, 40038948868, 40038952964, 40038957060, 40038965252,
        40038977540, 40038985732, 40038989828, 40039014404, 40047304708, 40047308804, 40047390724, 40047407108, 40055672836, 40055681028, 40055685124, 40055693316,
        40055697412, 40055713796, 40055717892, 40055721988, 40055726084, 40055750660, 40055754756, 40055762948, 40055771140, 40055775236, 40055779332, 40055783428,
        40055787524, 40064065540, 40064069636, 40064077828, 40064081924, 40064086020, 40064094212, 40064102404, 40064106500, 40064110596, 40064114692, 40064118788,
        40064122884, 40064126980, 40064131076, 40064135172, 40064139268, 40064143364, 40064147460, 40064151556, 40064155652, 40064163844, 40064172036, 40064176132,
        40072445956, 40072458244, 40072466436, 40072470532, 40072474628, 40072478724, 40072486916, 40072495108, 40072503300, 40072507396, 40072511492, 40072515588,
        40072519684, 40072523780, 40072527876, 40072536068, 40072540164, 40072544260, 40072548356, 40072552452, 40072556548, 40072564740, 40072568836, 40072572932,
        40072581124, 40080842756, 40080846852, 40080850948, 40080855044, 40080859140, 40080867332, 40080871428, 40080879620, 40080887812, 40080891908, 40080896004,
        40080900100, 40080904196, 40080908292, 40080916484, 40080920580, 40080924676, 40080928772, 40080932868, 40080936964, 40080957444, 40089223172, 40089227268,
        40089247748, 40089251844, 40089255940, 40089276420, 40089313284, 40089329668, 40089350148, 40089354244, 40097644548, 40097660932, 40097705988, 40114429956,
        40114454532, 40114458628, 40114470916, 40114479108, 40114487300, 40114495492, 40114499588, 40114507780, 40114515972, 40114524164, 40122777604, 40122781700,
        40122789892, 40122793988, 40122806276, 40122810372, 40122814468, 40122843140, 40122847236, 40122851332, 40122855428, 40122859524, 40122867716, 40122871812,
        40122880004, 40122888196, 40122892292, 40122900484, 40122904580, 40122908676, 40122912772, 40131170308, 40131174404, 40131178500, 40131182596, 40131186692,
        40131190788, 40131194884, 40131203076, 40131207172, 40131211268, 40131215364, 40131227652, 40131231748, 40131235844, 40131239940, 40131244036, 40131248132,
        40131252228, 40131256324, 40131260420, 40131264516, 40131272708, 40131276804, 40131280900, 40131284996, 40131289092, 40131293188, 40139554820, 40139558916,
        40139567108, 40139571204, 40139579396, 40139583492, 40139587588, 40139591684, 40139595780, 40139599876, 40139603972, 40139612164, 40139616260, 40139620356,
        40139628548, 40139632644, 40139636740, 40139644932, 40139649028, 40139653124, 40139657220, 40139665412, 40139669508, 40139673604, 40139677700, 40139681796,
        40139685892, 40147943428, 40147947524, 40147955716, 40147959812, 40147968004, 40147972100, 40147984388, 40147988484, 40147992580, 40147996676, 40148008964,
        40148013060, 40148021252, 40148045828, 40248745988, 40257048580, 40257060868, 40257064964, 40257073156, 40257081348, 40257093636, 40257097732, 40257105924,
        40257122308, 40257126404, 40257130500, 40257134596, 40265383940, 40265388036, 40265392132, 40265396228, 40265408516, 40265420804, 40265424900, 40265428996,
        40265441284, 40265445380, 40265453572, 40265461764, 40265465860, 40265469956, 40265474052, 40265478148, 40265490436, 40265494532, 40265498628, 40265502724,
        40265506820, 40265510916, 40273772548, 40273776644, 40273780740, 40273793028, 40273797124, 40273801220, 40273805316, 40273809412, 40273817604, 40273833988,
        40273838084, 40273842180, 40273850372, 40273854468, 40273858564, 40273862660, 40273874948, 40273879044, 40273883140, 40273887236, 40273891332, 40273895428,
        40273899524, 40273903620, 40273907716, 40282165252, 40282177540, 40282181636, 40282185732, 40282189828, 40282198020, 40282202116, 40282206212, 40282214404,
        40282218500, 40282222596, 40282226692, 40282234884, 40282238980, 40282243076, 40282247172, 40282251268, 40282255364, 40282259460, 40282263556, 40282267652,
        40282275844, 40282279940, 40282284036, 40282288132, 40282292228, 40290549764, 40290562052, 40290570244, 40290574340, 40290578436, 40290582532, 40290586628,
        40290594820, 40290598916, 40290603012, 40290607108, 40290611204, 40290615300, 40290619396, 40290623492, 40290627588, 40290635780, 40290639876, 40290643972,
        40290652164, 40290660356, 40290664452, 40290668548, 40290672644, 40290676740, 40290680836, 40298942468, 40298946564, 40298950660, 40298958852, 40298962948,
        40298967044, 40298975236, 40298979332, 40298983428, 40298987524, 40299003908, 40299012100, 40299016196, 40299020292, 40299028484, 40299032580, 40299036676,
        40299040772, 40299044868, 40299048964, 40299053060, 40299061252, 40299065348, 40307326980, 40307331076, 40307335172, 40307339268, 40307343364, 40307351556,
        40307359748, 40307363844, 40307367940, 40307372036, 40307376132, 40307384324, 40307388420, 40307392516, 40307396612, 40307400708, 40307404804, 40307412996,
        40307417092, 40307421188, 40307425284, 40307429380, 40307433476, 40307437572, 40307441668, 40307445764, 40307449860, 40307453956, 40315715588, 40315723780,
        40315727876, 40315731972, 40315740164, 40315744260, 40315748356, 40315752452, 40315768836, 40315772932, 40315777028, 40315781124, 40315785220, 40315789316,
        40315797508, 40315801604, 40315805700, 40315813892, 40315822084, 40315830276, 40315834372, 40315842564, 40315850756, 40324104196, 40324108292, 40324120580,
        40324153348, 40324165636, 40324173828, 40324182020, 40324186116, 40324210692, 40332492804, 40332529668, 40332541956, 40340889604, 40408035332, 40408064004,
        40408068100, 40408072196, 40408076292, 40408084484, 40408096772, 40408100868, 40408104964, 40408109060, 40408117252, 40408121348, 40408125444, 40416378884,
        40416391172, 40416395268, 40416399364, 40416403460, 40416411652, 40416415748, 40416419844, 40416423940, 40416428036, 40416432132, 40416436228, 40416440324,
        40416448516, 40416452612, 40416456708, 40416460804, 40416464900, 40416468996, 40416473092, 40416481284, 40416489476, 40416493572, 40416497668, 40416505860,
        40416509956, 40424767492, 40424775684, 40424779780, 40424787972, 40424796164, 40424800260, 40424808452, 40424816644, 40424820740, 40424824836, 40424828932,
        40424833028, 40424837124, 40424841220, 40424845316, 40424849412, 40424853508, 40424857604, 40424861700, 40424869892, 40424873988, 40424878084, 40424882180,
        40424886276, 40424890372, 40424894468, 40424898564, 40433156100, 40433160196, 40433164292, 40433168388, 40433176580, 40433180676, 40433184772, 40433188868,
        40433192964, 40433201156, 40433209348, 40433213444, 40433217540, 40433221636, 40433225732, 40433233924, 40433238020, 40433242116, 40433250308, 40433266692,
        40433278980, 40433283076, 40433287172, 40441552900, 40441556996, 40441565188, 40441573380, 40441589764, 40441597956, 40441614340, 40441630724, 40441643012,
        40449961988, 40449998852, 40450002948, 40491925508, 40491982852, 40491986948, 40491995140, 40492015620, 40500264964, 40500281348, 40500285444, 40500346884,
        40500350980, 40500355076, 40500359172, 40500363268, 40500367364, 40500383748, 40500387844, 40508657668, 40508702724, 40508723204, 40517070852, 40517144580,
        40525447172, 40525479940, 40525484036, 40525553668, 40533860356, 40567427076, 40567431172, 40567463940, 40567472132, 40567480324, 40567488516, 40567500804,
        40567504900, 40567508996, 40575762436, 40575766532, 40575770628, 40575782916, 40575787012, 40575791108, 40575795204, 40575799300, 40575807492, 40575811588,
        40575819780, 40575823876, 40575832068, 40575836164, 40575840260, 40575844356, 40575852548, 40575856644, 40575860740, 40575864836, 40575868932, 40575881220,
        40575893508, 40584151044, 40584159236, 40584163332, 40584175620, 40584179716, 40584183812, 40584192004, 40584196100, 40584204292, 40584208388, 40584212484,
        40584216580, 40584224772, 40584228868, 40584232964, 40584237060, 40584245252, 40584249348, 40584253444, 40584257540, 40584265732, 40584269828, 40584273924,
        40584278020, 40584282116, 40592539652, 40592551940, 40592556036, 40592564228, 40592568324, 40592572420, 40592576516, 40592580612, 40592584708, 40592588804,
        40592592900, 40592601092, 40592605188, 40592609284, 40592613380, 40592617476, 40592629764, 40592637956, 40592642052, 40592646148, 40592650244, 40592654340,
        40592662532, 40592666628, 40600928260, 40600932356, 40600936452, 40600944644, 40600948740, 40600952836, 40600956932, 40600961028, 40600965124, 40600969220,
        40600973316, 40600977412, 40600981508, 40600985604, 40600989700, 40600993796, 40600997892, 40601006084, 40601010180, 40601018372, 40601022468, 40601055236,
        40601063428, 40609316868, 40609320964, 40609325060, 40609329156, 40609337348, 40609341444, 40609345540, 40609349636, 40609353732, 40609357828, 40609366020,
        40609374212, 40609386500, 40609390596, 40609394692, 40609406980, 40609415172, 40609419268, 40609431556, 40609447940, 40617713668, 40617721860, 40617725956,
        40617738244, 40617750532, 40617758724, 40617795588, 40626102276, 40634552324, 40634564612, 40634609668, 40642871300, 40642875396, 40642895876, 40642916356,
        40642920452, 40642940932, 40642945028, 40642949124, 40642953220, 40642961412, 40642965508, 40642969604, 40642973700, 40642990084, 40651264004, 40651268100,
        40651272196, 40651276292, 40651280388, 40651284484, 40651288580, 40651292676, 40651296772, 40651309060, 40651317252, 40651329540, 40651337732, 40651341828,
        40651345924, 40651350020, 40651358212, 40651362308, 40651366404, 40651374596, 40651382788, 40651386884, 40651390980, 40659652612, 40659660804, 40659668996,
        40659677188, 40659681284, 40659685380, 40659689476, 40659693572, 40659697668, 40659701764, 40659705860, 40659709956, 40659714052, 40659718148, 40659722244,
        40659730436, 40659734532, 40659738628, 40659742724, 40659746820, 40659750916, 40659767300, 40659771396, 40659779588, 40668037124, 40668041220, 40668045316,
        40668049412, 40668053508, 40668057604, 40668069892, 40668073988, 40668078084, 40668082180, 40668086276, 40668090372, 40668094468, 40668098564, 40668102660,
        40668106756, 40668114948, 40668123140, 40668135428, 40668139524, 40668143620, 40668151812, 40668160004, 40668168196, 40676425732, 40676433924, 40676446212,
        40676450308, 40676454404, 40676458500, 40676462596, 40676466692, 40676470788, 40676487172, 40676540420, 40676560900, 40684818436, 40684822532, 40684847108,
        40701607940, 40701640708, 40701652996, 40701669380, 40701681668, 40701685764, 40701710340, 40701718532, 40701722628, 40701726724, 40709984260, 40709988356,
        40709996548, 40710000644, 40710004740, 40710017028, 40710025220, 40710033412, 40710041604, 40710053892, 40710057988, 40710062084, 40710066180, 40710070276,
        40710074372, 40710078468, 40710082564, 40710094852, 40710098948, 40710103044, 40710107140, 40710111236, 40710115332, 40718381060, 40718385156, 40718397444,
        40718401540, 40718405636, 40718413828, 40718417924, 40718422020, 40718430212, 40718434308, 40718442500, 40718446596, 40718450692, 40718454788, 40718462980,
        40718467076, 40718471172, 40718475268, 40718479364, 40718483460, 40718487556, 40718491652, 40718499844, 40726757380, 40726765572, 40726777860, 40726781956,
        40726786052, 40726790148, 40726794244, 40726798340, 40726802436, 40726806532, 40726810628, 40726814724, 40726818820, 40726827012, 40726831108, 40726835204,
        40726839300, 40726843396, 40726847492, 40726851588, 40726855684, 40726859780, 40726863876, 40726867972, 40726872068, 40726876164, 40726880260, 40726884356,
        40735150084, 40735154180, 40735170564, 40735174660, 40735178756, 40735182852, 40735186948, 40735191044, 40735195140, 40735244292, 40735256580, 40735268868,
        40743600132, 40785580036, 40944930820, 40944988164, 40953298948, 40953344004, 40961671172, 41028870148, 41037172740, 41037176836, 41037180932, 41037185028,
        41037193220, 41037201412, 41037213700, 41037221892, 41037230084, 41037246468, 41037254660, 41045544964, 41045549060, 41045557252, 41045565444, 41045585924,
        41045594116, 41045606404, 41045630980, 41054044164, 41179856900, 41188147204, 41188151300, 41188155396, 41188163588, 41188167684, 41188175876, 41188184068,
        41188188164, 41188192260, 41188216836, 41188220932, 41188233220, 41188237316, 41188245508, 41188253700, 41196539908, 41196544004, 41196548100, 41196564484,
        41196589060, 41196617732, 41204912132, 41204981764, 41205010436, 41213341700, 42740158468, 42916184068, 43184680964, 43234951172, 43251908612, 43478392836,
        43696492548, 43981688836, 44140957700, 44384305156, 44409368580, 44510138372, 44644372484, 44853960708, 44853981188, 44921073668, 45021822980, 45181190148,
        45315354628, 45323857924, 45357314052, 45575553028, 45776842756, 45802029060, 45877514244, 45936136196, 45944619012, 45969793028, 46129168388, 46145875972,
        46271676420, 46380847108, 46397636612, 46481494020, 46531809284, 46632517636, 46699470852, 47001530372, 47018283012, 47093735428, 47127326724, 47228116996,
        47244877828, 47378980868, 47613886468, 47840378884, 48008130564, 48083574788, 48100392964, 48410738692, 48528224260, 48695967748, 48729538564, 48738021380,
        48738062340, 48888946692, 49182576644, 49232863236, 49300045828, 49300086788, 49434292228, 49450909700, 49484603396, 49509699588, 49543192580, 49752928260,
        49786486788, 49786560516, 49912381444, 50029735940, 50038267908, 50172358660, 50507890692, 50575114244, 51237687300, 51783008260, 52101775364, 52378701828,
        52496080900, 52554739716, 52605157380, 52798013444, 52915560452, 52974166020, 53041360900, 53158838276, 53175459844, 53225852932, 53292892164, 53443944452,
        53494398980, 53569863684, 53586542596, 53670531076, 53771157508, 53804683268, 53897056260, 53964115972, 54006026244, 54349897732, 54358339588, 54551293956,
        54584803332, 54593265668, 54626758660, 54777749508, 54853341188, 55079841796, 55155191812, 55155249156, 55205654532, 55222280196, 55406948356, 55457267716,
        55482318852, 55524315140, 55574720516, 55616618500, 55641821188, 55692152836, 55767642116, 55801171972, 55834808324, 55977299972, 56002510852, 56111628292,
        56187064324, 56254246916, 56346509316, 56413618180, 56447123460, 56472195076, 56505884676, 56531066884, 56681906180, 56698851332, 56807735300, 56916881412,
        56925319172, 56992337924, 56992378884, 57000816644, 57034272772, 57109708804, 57445273604, 57688682500, 57772466180, 57831280644, 57990549508, 58007330820,
        58124836868, 58250674180, 58275844100, 58359734276, 58577793028, 58678530052, 59676647428, 60012281860, 60339437572, 60515504132, 60725211140, 60867846148,
        60918210564, 61044092932, 61220175876, 61245390852, 61765529604, 61941583876, 61975261188, 62235185156, 62436593668, 62478426116, 62587645956, 62730194948,
        62981853188, 63099183108, 63409553412, 63887740932, 64055582724, 64063987716, 64072404996, 64114204676, 64172924932, 64231690244, 64357634052, 64525303812,
        64542097412, 64735133700, 64928018436, 65053847556, 65087422468, 65171267588, 65171279876, 65338961924, 65364271108, 65615896580, 65632710660, 65817124868,
        66043662340, 66286854148, 66286931972, 66781859844, 66874085380, 66924482564, 67251617796, 67788517380, 67897503748, 67947847684, 68191072260, 68199591940,
        68274982916, 68434468868, 68493156356, 68644114436, 68744785924, 68769959940, 68895756292, 68988014596, 69440970756, 69491388420, 69499711492, 69516496900,
        69944393732, 71362035716, 71445893124, 71454334980, 71546630148, 72075001860, 72276480004, 72402194436, 72528121860, 72569913348, 72611962884, 73132097540,
        73291378692, 73844989956, 74180706308, 74289750020, 74776276996, 75514445828, 75598327812, 75606626308, 75657068548, 76000968708, 76017807364, 76135235588,
        76932075524, 77066379268, 77124927492, 77183696900, 77267693572, 78173515780, 78626553860, 78693666820, 78752415748, 78878314500, 78953660420, 79020888068,
        79104770052, 79616380932, 79775866884, 79842885636, 79910105092, 80186814468, 80421797892, 80606384132, 80614772740, 80748838916, 81159876612, 81436766212,
        81554231300, 81688473604, 81923256324, 81981976580, 82434916356, 83055833092, 83173220356, 83265449988, 83324284932, 83391262724, 83458351108, 83466854404,
        83609395204
    };

    [RavenFact(RavenTestCategory.Voron)]
    public unsafe void CanProperlyEncodeValues()
    {
        var buffer = stackalloc byte[10000];
        var entries = stackalloc long[1024];
        using var allocator = new ByteStringContext(SharedMultipleUseFlag.None);
        int sizeUsed;
        fixed (long* l = Data)
        {
            using var encoder = new FastPForEncoder(allocator);
            encoder.Encode(l, Data.Length);
            (int count, sizeUsed) = encoder.Write(buffer, 10_000);
            Assert.Equal(Data.Length, count);
        }

        using var reader = new FastPForDecoder(allocator);
        reader.Init(buffer, sizeUsed);
        int idx = 0;
        while (true)
        {
            var read = reader.Read(entries, 1024);
            if (read == 0)
                break;
            for (int i = 0; i < read; i++)
            {
                Assert.Equal(Data[idx++], entries[i]);
            }
        }
    }


    public SimdCompressionEncodingTests(ITestOutputHelper output) : base(output)
    {
    }
}
